package com.itcast.tanhuaserver.controller;

import com.itcast.tanhuaserver.domain.Announcement;
import com.itcast.tanhuaserver.service.IMService;
import com.itcast.tanhuaserver.utils.NoAuthorization;
import com.itcast.tanhuaserver.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuaserver.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/2215:08
 * @Version 1.0
 */
@RestController
@RequestMapping("messages")
@Slf4j
public class IMController {

    @Autowired
    private IMService imService;
    /**
     * 添加好友
     *
     * @param parm
     * @return
     */
    @PostMapping("contacts")
    public ResponseEntity<Void> contactUser(@RequestBody Map<String,Object> parm){
        try {
            Long userId = Long.valueOf(parm.get("userId").toString());
            Boolean bool =  this.imService.contactUser(userId);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (NumberFormatException e) {
            log.error("添加好友错误~ userId=" + parm.get("userId"),e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询联系人列表
     *
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("contacts")
    public ResponseEntity<PageResult> queryContactsList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "keyword",required = false) String keyword){
        try {
            PageResult pageResult = this.imService.queryContactsList(page,pageSize,keyword);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("环信查询联系人列表错误~ ",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询点赞列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("likes")
    public ResponseEntity<PageResult> queryMessageLikeList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                           @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = this.imService.queryMessageLikeList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("环信查询点赞列表错误",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询评论列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("comments")
    public ResponseEntity<PageResult> queryMessageCommentList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                              @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = this.imService.queryMessageCommentList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("环信查询评论列表错误",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询点赞列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("loves")
    public ResponseEntity<PageResult> queryMessageLoveList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                           @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = this.imService.queryMessageLoveList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("环信查询评论列表错误",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询公告列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("announcements")
    @NoAuthorization
    public ResponseEntity<PageResult> queryMessageAnnouncementList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                                     @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = this.imService.queryMessageAnnouncementList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.error("环信查询评论列表错误",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
